home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / UnsignedNumberDocument.java < prev    next >
Text File  |  1998-09-10  |  3KB  |  115 lines

  1. package com.symantec.itools.swing.models;
  2.  
  3. import com.sun.java.swing.text.AttributeSet;
  4. import com.sun.java.swing.text.BadLocationException;
  5. import com.sun.java.swing.text.PlainDocument;
  6. import com.sun.java.swing.text.Segment;
  7. import com.sun.java.swing.text.StringContent;
  8.  
  9. /*
  10.  * Copyright (c) 1998 Symantec Corporation. All Rights Reserved.
  11.  *
  12.  * NumberDocument extends PlainDocument and is supposed to be used as
  13.  * a model in JFC text components where only unsigned numerical values
  14.  * make sense. When this model is used, the user can only enter digits
  15.  * in the JFC text component.
  16.  */
  17.  
  18. public class UnsignedNumberDocument extends PlainDocument
  19. {
  20.     private boolean defaultValueEnabled = false;
  21.     private int defaultValue = 0;
  22.  
  23.     /*
  24.      * Inserts a string of content. The string will be inserted only if
  25.      * it contains valid characters. The string will remplace current
  26.      * value if the later is equal to the default value.
  27.      */
  28.     public void insertString (int offset, String string,
  29.                               AttributeSet attributeSet)
  30.         throws BadLocationException
  31.     {
  32.         // Check string to be inserted to ensure each character is valid.
  33.         for (int characterIndex = 0; characterIndex < string.length ();
  34.              characterIndex++) {
  35.             if (!Character.isDigit (string.charAt (characterIndex))) {
  36.                 // Oups, an invalid character. We won't insert this string.
  37.                 return;
  38.             }
  39.         }
  40.         // Check if current value is default value.
  41.          if (defaultValueEnabled && getLength () == 1) {
  42.              if (getText (0, 1).equals (String.valueOf (defaultValue))) {
  43.                  super.remove (0, 1);
  44.                  super.insertString (0, string, attributeSet);
  45.              } else {
  46.                  super.insertString(offset, string, attributeSet);
  47.              }
  48.          } else {
  49.              super.insertString(offset, string, attributeSet);
  50.          }
  51.     }
  52.  
  53.     /*
  54.      * Set the default value returned by <CODE>getText</CODE> and
  55.      * <CODE>getValue</CODE>.
  56.      *
  57.      * @see getText
  58.      * @see getValue
  59.      */
  60.     public void setDefaultValue (int defaultValue)
  61.     {
  62.         if (defaultValueEnabled &&
  63.             String.valueOf (defaultValue).length () != 1) {
  64.             throw new IllegalArgumentException
  65.                 ("Default value must have exactly one digit");
  66.         }
  67.         this.defaultValue = defaultValue;
  68.     }
  69.  
  70.      /*
  71.       * Gives the default value returned by <CODE>getText</CODE> and
  72.       * <CODE>getValue</CODE>.
  73.       *
  74.       * @see getText
  75.       * @see getValue
  76.       */
  77.      public int getDefaultValue ()
  78.      {
  79.          return defaultValue;
  80.      }
  81.  
  82.     /*
  83.      * Enable of desable the default value. If the default value is
  84.      * enabled, a user won't be able to remove all digits and thus the
  85.      * value will at least contain the default value.
  86.      */
  87.     public void setDefaultValueEnabled (boolean defaultValueEnabled)
  88.     {
  89.         this.defaultValueEnabled = defaultValueEnabled;
  90.     }
  91.  
  92.     /*
  93.      * Returns true if the default value is enabled, false otherwise.
  94.      *
  95.      * @see setDefaultValueEnabled
  96.      */
  97.     public boolean isDefaultValueEnabled ()
  98.     {
  99.         return defaultValueEnabled;
  100.     }
  101.  
  102.     /*
  103.      * Override superclass <CODE>remove</CODE> method to prevent
  104.      * removing all digits if default value is enabled.
  105.      */
  106.     public void remove (int offset, int length)
  107.         throws BadLocationException
  108.     {
  109.         super.remove (offset, length);
  110.         if (defaultValueEnabled && getLength () == 0) {
  111.             insertString (0, String.valueOf (defaultValue), null);
  112.         }
  113.     }
  114. }
  115.